package com.xique.park.controller.screen;

import com.xique.common.core.enums.AreaLevel;
import com.xique.common.core.web.domain.AjaxResult;
import com.xique.common.log.annotation.Log;
import com.xique.park.bean.request.screen.ScreenRequest;
import com.xique.park.bean.vo.screen.ScreenTreeVo;
import com.xique.park.bean.vo.screen.ScreenVo;
import com.xique.park.bean.vo.web.parkinfo.ParkBaseInfoVo;
import com.xique.park.service.service.IScreenService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 地图数据大屏控制器
 *
 * @author wld
 * @date 2022/3/9 10:00
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/screen")
public class ScreenController {

    private final IScreenService screenService;

    @Log(title = "获取停车大屏总数据")
    @PostMapping(value = "/info")
    public AjaxResult info(@RequestBody @Validated ScreenRequest request) {
        handleRequest(request);
        ScreenVo screenVo = screenService.info(request.getCountry(), request.getProvince(), request.getCity(),
                request.getDistrict(), request.getParkInfoId());
        return AjaxResult.success(screenVo);
    }

    @Log(title = "获取区域内的停车场数量、今日收入以及支付方式")
    @PostMapping(value = "/areaBaseInfo")
    public AjaxResult areaBaseInfo(@RequestBody @Validated ScreenRequest request) {
        handleRequest(request);
        ScreenVo screenVo = screenService.areaBaseInfo(request.getCountry(), request.getProvince(), request.getCity(),
                request.getDistrict(), request.getParkInfoId());
        return AjaxResult.success(screenVo);
    }

    @Log(title = "查询省市区内的项目列表")
    @PostMapping(value = "/parkList")
    public AjaxResult parkList(@RequestBody @Validated ScreenRequest request) {
        handleRequest(request);
        List<ParkBaseInfoVo> parkInfoVoList = screenService.selectParkList(request.getCountry(), request.getProvince(), request.getCity(),
                request.getDistrict());
        return AjaxResult.success(parkInfoVoList);
    }

    @Log(title = "判断指定区域内是否有项目存在")
    @PostMapping(value = "/parkExist")
    public AjaxResult parkExist(@RequestBody @Validated ScreenRequest request) {
        handleRequest(request);
        List<Integer> parkExistVoList = screenService.selectParkExist(request.getCountry(), request.getProvince(), request.getCity(),
                request.getDistrict());
        return AjaxResult.success(parkExistVoList);
    }

    @Log(title = "获取地图显示的级别")
    @GetMapping(value = "/displayLevel")
    public AjaxResult displayLevel() {
        ScreenVo screenVo = screenService.selectDisplayLevel();
        if (null == screenVo) {
            return AjaxResult.error();
        }
        return AjaxResult.success(screenVo);
    }

    @Log(title = "获取项目树形结构")
    @GetMapping(value = "/tree")
    public AjaxResult tree() {
        ScreenTreeVo result = screenService.selectTree();
        return AjaxResult.success(result);
    }

    private void handleRequest(ScreenRequest request) {
        int parent = request.getParent();
        String level = request.getLevel();
        if (AreaLevel.COUNTRY.getLevel().equals(level)) {
            request.setCountry(parent);
        } else if (AreaLevel.PROVINCE.getLevel().equals(level)) {
            request.setProvince(parent);
        } else if (AreaLevel.CITY.getLevel().equals(level)) {
            request.setCity(parent);
        } else if (AreaLevel.DISTRICT.getLevel().equals(level)) {
            request.setDistrict(parent);
        } else if (AreaLevel.PROJECT.getLevel().equals(level)) {
            request.setParkInfoId((long) parent);
        } else {
            request.setCountry(100000);
        }
    }

}
